class SectionsController < ApplicationController
  #权限验证
  before_filter :auth
  layout "admin"  
  def index
    redirect_to :action=>'list'
  end

  def list    
    #提交表单为post
    if request.post?
      @section = params[:section]
      name = @section[:name]     
      channel_id = @section[:channel_id]     
      if !channel_id.empty?
        @sections = Section.paginate :per_page => $perPage, :page =>  params[:page],
          :conditions => ['name like ? and channel_id = ? ', "%#{name}%",channel_id],
          :order => 'channel_id,x,y asc'
      else
        @sections = Section.paginate :per_page => $perPage, :page =>  params[:page],
          :conditions => ['name like ? ', "%#{name}%"],
          :order => 'channel_id,x,y asc'
      end
      
    else
      @sections = Section.paginate  :page => params[:page],
        :per_page => $perPage,
        :order => "channel_id,x,y asc"
    end
    #查询项参数
    @section = Section.new
    @section.name = name
    @section.channel_id = channel_id
    #功能名
    @titles = "栏目列表"   
  end

  def show
    @section = Section.find(params[:id])
    @titles = "栏目详细信息---"+@section.name
  end

  def new
    @section = Section.new
    @titles = "新增栏目---"
  end

  def edit
    @section = Section.find(params[:id])
    @titles = "编辑栏目---"+@section.name
  end

  def new_al
    @section = Section.new
    @titles = "新增专辑---"
  end

  def edit_al
    @section = Section.find(params[:id])
    @titles = "编辑栏目---"+@section.name
  end
  
  def create
    @section = Section.new(params[:section])
    section = Section.find_all_by_level @section[:level]
    if  @section && @section.save && !section.nil? && @section.errors.empty?
      flash[:notice] = "新建资源成功"
      render :action => "new"
    else
      render :action => "new"
    end
  end

  def create_al
    @section = Section.new(params[:section])
    section = Section.find_all_by_level @section[:level]
    require 'ftools'
    require "fileutils.rb"
    upload = params[:upload]
    #获取上传的文件名称
    uploadFile=  upload['datafile'].original_filename
    #获取当前时期
    timeStr = Time.now.strftime('%Y/%m/%d')
    #上传相对路径
    rel_path = timeStr
    #上传全部路径
    path=File.join($upload_path,rel_path)
    #创建上传源文件夹
    File.makedirs path
    #上传的物理文件名
    file_upload_name = File.join(path, uploadFile)
    File.open(file_upload_name, "wb") {
      |f| f.write(upload['datafile'].read)
    }
    @section.pic = file_upload_name.gsub($upload_path, $img_path)
    if  @section && @section.save && !section.nil? && @section.errors.empty?
      flash[:notice] = "新建资源成功"
      render :action => "new_al"
    else
      render :action => "new_al"
    end
  end


  def update
    @section = params[:section]
    if Section.update(@section[:id], @section)
      flash[:notice] = '更新成功'
      redirect_to :action=>'index'
    else
      render :action => "edit"
    end    
  end

  def update_al
    @section = params[:section]

    unless params[:upload].nil?
      require 'ftools'
      require "fileutils.rb"
      upload = params[:upload]
      #获取上传的文件名称
      uploadFile=  upload['datafile'].original_filename
      #获取当前时期
      timeStr = Time.now.strftime('%Y/%m/%d')
      #上传相对路径
      rel_path = timeStr
      #上传全部路径
      path=File.join($upload_path,rel_path)
      #创建上传源文件夹
      File.makedirs path
      #上传的物理文件名
      file_upload_name = File.join(path, uploadFile)
      File.open(file_upload_name, "wb") {
        |f| f.write(upload['datafile'].read)
      }
      src_u = file_upload_name.gsub($upload_path, $img_path)    
    end
    if Section.update(@section[:id], @section)
      unless src_u.nil?
        se = Section.find @section[:id]
        se.update_attributes({:pic => src_u})
      end
      flash[:notice] = '更新成功'
      redirect_to :action=>'index'
    else
      render :action => "edit_al"
    end
  end

  def delete
    section = Section.find params[:id]
    ress = Resource.find_all_by_col_id section.id
    if ress.size==0
      section.destroy
    end
    redirect_to :action=>'index'
  end

end
